package cn.kgc.vue.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Score;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.LogInfo;
import cn.kgc.vue.service.LogInfoService;
import cn.kgc.vue.mapper.LogInfoMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author 11839
* @description 针对表【sys_log_info】的数据库操作Service实现
* @createDate 2024-06-28 15:23:44
*/
@Service
public class LogInfoServiceImpl extends ServiceImpl<LogInfoMapper, LogInfo>
    implements LogInfoService{

    @Override
    public ResponseResult logsPage(QueryCondition<LogInfo> queryCondition) {
        Page<LogInfo> logInfoPage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());

        LogInfo searchParams = queryCondition.getSearchParams();
        String username = searchParams.getUsername();
        String ip = searchParams.getIp();
        String model = searchParams.getModel();

        LambdaQueryWrapper<LogInfo> lambda = new QueryWrapper<LogInfo>().lambda();
        lambda.eq(StringUtils.isNotBlank(username),LogInfo::getUsername,username)
                .eq(StringUtils.isNotBlank(ip),LogInfo::getIp,ip)
                .eq(StringUtils.isNotBlank(model),LogInfo::getModel,model)
                .orderByDesc(LogInfo::getCreateTime);


        baseMapper.selectPage(logInfoPage, lambda);

        List<LogInfo> records = logInfoPage.getRecords();
        long total = logInfoPage.getTotal();



        return ResponseResult.success().data("total", total).data("tableData", records);
    }

    @Override
    public ResponseResult exportLogs(LogInfo logInfo, HttpServletResponse response) throws IOException {
        LambdaQueryWrapper<LogInfo> lambda = new QueryWrapper<LogInfo>().lambda();
        String ip = logInfo.getIp();
        String username = logInfo.getUsername();
        String model = logInfo.getModel();
        lambda.eq(StringUtils.isNotBlank(ip),LogInfo::getIp,ip)
                .eq(StringUtils.isNotBlank(username),LogInfo::getUsername,username)
                .eq(StringUtils.isNotBlank(model),LogInfo::getModel,model);

        List<LogInfo> logInfos = baseMapper.selectList(lambda);


        //创建导出参数
        ExportParams exportParams = new ExportParams();
        exportParams.setTitle("日志表");
        Date date = new Date();
        exportParams.setSheetName((date.getMonth()+1) +"月"+date.getDate()+"日"+"日志表");

        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, LogInfo.class, logInfos);
        ServletOutputStream outputStream = response.getOutputStream();

        // 写出工作簿
        sheets.write(outputStream);
        sheets.close();
        outputStream.close();

        return null;
    }
}




